home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / noutline.el.z / noutline.el
Lisp/Scheme  |  1998-10-27  |  24KB  |  655 lines

  1. ;;; outline.el --- outline mode commands for Emacs
  2.  
  3. ;; Copyright (C) 1986, 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: outlines
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; This package is a major mode for editing outline-format documents.
  28. ;; An outline can be `abstracted' to show headers at any given level,
  29. ;; with all stuff below hidden.  See the Emacs manual for details.
  30.  
  31. ;;; Code:
  32.  
  33. ;; Jan '86, Some new features added by Peter Desnoyers and rewritten by RMS.
  34.   
  35. (defvar outline-regexp nil
  36.   "*Regular expression to match the beginning of a heading.
  37. Any line whose beginning matches this regexp is considered to start a heading.
  38. The recommended way to set this is with a Local Variables: list
  39. in the file it applies to.  See also outline-heading-end-regexp.")
  40.  
  41. ;; Can't initialize this in the defvar above -- some major modes have
  42. ;; already assigned a local value to it.
  43. (or (default-value 'outline-regexp)
  44.     (setq-default outline-regexp "[*\^L]+"))
  45.   
  46. (defvar outline-heading-end-regexp "\n"
  47.   "*Regular expression to match the end of a heading line.
  48. You can assume that point is at the beginning of a heading when this
  49. regexp is searched for.  The heading ends at the end of the match.
  50. The recommended way to set this is with a `Local Variables:' list
  51. in the file it applies to.")
  52.  
  53. (defvar outline-mode-prefix-map nil)
  54.  
  55. (if outline-mode-prefix-map
  56.     nil
  57.   (setq outline-mode-prefix-map (make-sparse-keymap))
  58.   (define-key outline-mode-prefix-map "\C-n" 'outline-next-visible-heading)
  59.   (define-key outline-mode-prefix-map "\C-p" 'outline-previous-visible-heading)
  60.   (define-key outline-mode-prefix-map "\C-i" 'show-children)
  61.   (define-key outline-mode-prefix-map "\C-s" 'show-subtree)
  62.   (define-key outline-mode-prefix-map "\C-d" 'hide-subtree)
  63.   (define-key outline-mode-prefix-map "\C-u" 'outline-up-heading)
  64.   (define-key outline-mode-prefix-map "\C-f" 'outline-forward-same-level)
  65.   (define-key outline-mode-prefix-map "\C-b" 'outline-backward-same-level)
  66.   (define-key outline-mode-prefix-map "\C-t" 'hide-body)
  67.   (define-key outline-mode-prefix-map "\C-a" 'show-all)
  68.   (define-key outline-mode-prefix-map "\C-c" 'hide-entry)
  69.   (define-key outline-mode-prefix-map "\C-e" 'show-entry)
  70.   (define-key outline-mode-prefix-map "\C-l" 'hide-leaves)
  71.   (define-key outline-mode-prefix-map "\C-k" 'show-branches)
  72.   (define-key outline-mode-prefix-map "\C-q" 'hide-sublevels)
  73.   (define-key outline-mode-prefix-map "\C-o" 'hide-other))
  74.  
  75. (defvar outline-mode-menu-bar-map nil)
  76. (if outline-mode-menu-bar-map
  77.     nil
  78.   (setq outline-mode-menu-bar-map (make-sparse-keymap))
  79.  
  80.   (define-key outline-mode-menu-bar-map [hide]
  81.     (cons "Hide" (make-sparse-keymap "Hide")))
  82.  
  83.   (define-key outline-mode-menu-bar-map [hide hide-other]
  84.     '("Hide Other" . hide-other))
  85.   (define-key outline-mode-menu-bar-map [hide hide-sublevels]
  86.     '("Hide Sublevels" . hide-sublevels))
  87.   (define-key outline-mode-menu-bar-map [hide hide-subtree]
  88.     '("Hide Subtree" . hide-subtree))
  89.   (define-key outline-mode-menu-bar-map [hide hide-entry]
  90.     '("Hide Entry" . hide-entry))
  91.   (define-key outline-mode-menu-bar-map [hide hide-body]
  92.     '("Hide Body" . hide-body))
  93.   (define-key outline-mode-menu-bar-map [hide hide-leaves]
  94.     '("Hide Leaves" . hide-leaves))
  95.  
  96.   (define-key outline-mode-menu-bar-map [show]
  97.     (cons "Show" (make-sparse-keymap "Show")))
  98.  
  99.   (define-key outline-mode-menu-bar-map [show show-subtree]
  100.     '("Show Subtree" . show-subtree))
  101.   (define-key outline-mode-menu-bar-map [show show-children]
  102.     '("Show Children" . show-children))
  103.   (define-key outline-mode-menu-bar-map [show show-branches]
  104.     '("Show Branches" . show-branches))
  105.   (define-key outline-mode-menu-bar-map [show show-entry]
  106.     '("Show Entry" . show-entry))
  107.   (define-key outline-mode-menu-bar-map [show show-all]
  108.     '("Show All" . show-all))
  109.  
  110.   (define-key outline-mode-menu-bar-map [headings]
  111.     (cons "Headings" (make-sparse-keymap "Headings")))
  112.  
  113.   (define-key outline-mode-menu-bar-map [headings outline-backward-same-level]
  114.     '("Previous Same Level" . outline-backward-same-level))
  115.   (define-key outline-mode-menu-bar-map [headings outline-forward-same-level]
  116.     '("Next Same Level" . outline-forward-same-level))
  117.   (define-key outline-mode-menu-bar-map [headings outline-previous-visible-heading]
  118.     '("Previous" . outline-previous-visible-heading))
  119.   (define-key outline-mode-menu-bar-map [headings outline-next-visible-heading]
  120.     '("Next" . outline-next-visible-heading))
  121.   (define-key outline-mode-menu-bar-map [headings outline-up-heading]
  122.     '("Up" . outline-up-heading)))
  123.  
  124. (defvar outline-mode-map nil "")
  125.  
  126. (if outline-mode-map
  127.     nil
  128.   (setq outline-mode-map (nconc (make-sparse-keymap) text-mode-map))
  129.   (define-key outline-mode-map "\C-c" outline-mode-prefix-map)
  130.   (define-key outline-mode-map [menu-bar] outline-mode-menu-bar-map))
  131.  
  132. (defvar outline-minor-mode nil
  133.   "Non-nil if using Outline mode as a minor mode of some other mode.")
  134. (make-variable-buffer-local 'outline-minor-mode)
  135. (or (assq 'outline-minor-mode minor-mode-alist)
  136.     (setq minor-mode-alist (append minor-mode-alist
  137.                    (list '(outline-minor-mode " Outl")))))
  138.  
  139. (defvar outline-font-lock-keywords
  140.   '(;; Highlight headings according to the level.
  141.     ("^\\(\\*+\\)[ \t]*\\(.+\\)?[ \t]*$"
  142.      (1 font-lock-string-face)
  143.      (2 (let ((len (- (match-end 1) (match-beginning 1))))
  144.       (or (cdr (assq len '((1 . font-lock-function-name-face)
  145.                    (2 . font-lock-keyword-face)
  146.                    (3 . font-lock-comment-face))))
  147.           font-lock-variable-name-face))
  148.     nil t))
  149.     ;; Highlight citations of the form [1] and [Mar94].
  150.     ("\\[\\([A-Z][A-Za-z]+\\)*[0-9]+\\]" . font-lock-type-face))
  151.   "Additional expressions to highlight in Outline mode.")
  152.  
  153. (defvar outline-view-change-hook nil
  154.   "Normal hook to be run after outline visibility changes.")
  155.  
  156. ;;;autoload
  157. (defun outline-mode ()
  158.   "Set major mode for editing outlines with selective display.
  159. Headings are lines which start with asterisks: one for major headings,
  160. two for subheadings, etc.  Lines not starting with asterisks are body lines. 
  161.  
  162. Body text or subheadings under a heading can be made temporarily
  163. invisible, or visible again.  Invisible lines are attached to the end 
  164. of the heading, so they move with it, if the line is killed and yanked
  165. back.  A heading with text hidden under it is marked with an ellipsis (...).
  166.  
  167. Commands:\\<outline-mode-map>
  168. \\[outline-next-visible-heading]   outline-next-visible-heading      move by visible headings
  169. \\[outline-previous-visible-heading]   outline-previous-visible-heading
  170. \\[outline-forward-same-level]   outline-forward-same-level        similar but skip subheadings
  171. \\[outline-backward-same-level]   outline-backward-same-level
  172. \\[outline-up-heading]   outline-up-heading            move from subheading to heading
  173.  
  174. \\[hide-body]    make all text invisible (not headings).
  175. \\[show-all]    make everything in buffer visible.
  176.  
  177. The remaining commands are used when point is on a heading line.
  178. They apply to some of the body or subheadings of that heading.
  179. \\[hide-subtree]   hide-subtree    make body and subheadings invisible.
  180. \\[show-subtree]   show-subtree    make body and subheadings visible.
  181. \\[show-children]   show-children    make direct subheadings visible.
  182.          No effect on body, or subheadings 2 or more levels down.
  183.          With arg N, affects subheadings N levels down.
  184. \\[hide-entry]       make immediately following body invisible.
  185. \\[show-entry]       make it visible.
  186. \\[hide-leaves]       make body under heading and under its subheadings invisible.
  187.              The subheadings remain visible.
  188. \\[show-branches]  make all subheadings at all levels visible.
  189.  
  190. The variable `outline-regexp' can be changed to control what is a heading.
  191. A line is a heading if `outline-regexp' matches something at the
  192. beginning of the line.  The longer the match, the deeper the level.
  193.  
  194. Turning on outline mode calls the value of `text-mode-hook' and then of
  195. `outline-mode-hook', if they are non-nil."
  196.   (interactive)
  197.   (kill-all-local-variables)
  198.   (use-local-map outline-mode-map)
  199.   (setq mode-name "Outline")
  200.   (setq major-mode 'outline-mode)
  201.   (define-abbrev-table 'text-mode-abbrev-table ())
  202.   (setq local-abbrev-table text-mode-abbrev-table)
  203.   (set-syntax-table text-mode-syntax-table)
  204.   (make-local-variable 'line-move-ignore-invisible)
  205.   (setq line-move-ignore-invisible t)
  206.   ;; Cause use of ellipses for invisible text.
  207.   (setq buffer-invisibility-spec '((t . t)))
  208.   (make-local-variable 'paragraph-start)
  209.   (setq paragraph-start (concat paragraph-start "\\|\\("
  210.                 outline-regexp "\\)"))
  211.   ;; Inhibit auto-filling of header lines.
  212.   (make-local-variable 'auto-fill-inhibit-regexp)
  213.   (setq auto-fill-inhibit-regexp outline-regexp)
  214.   (make-local-variable 'paragraph-separate)
  215.   (setq paragraph-separate (concat paragraph-separate "\\|\\("
  216.                    outline-regexp "\\)"))
  217.   (make-local-variable 'font-lock-defaults)
  218.   (setq font-lock-defaults '(outline-font-lock-keywords t))
  219.   (make-local-variable 'change-major-mode-hook)
  220.   (add-hook 'change-major-mode-hook 'show-all)
  221.   (run-hooks 'text-mode-hook 'outline-mode-hook))
  222.  
  223. (defvar outline-minor-mode-prefix "\C-c@"
  224.   "*Prefix key to use for Outline commands in Outline minor mode.
  225. The value of this variable is checked as part of loading Outline mode.
  226. After that, changing the prefix key requires manipulating keymaps.")
  227.  
  228. (defvar outline-minor-mode-map nil)
  229. (if outline-minor-mode-map
  230.     nil
  231.   (setq outline-minor-mode-map (make-sparse-keymap))
  232.   (define-key outline-minor-mode-map [menu-bar]
  233.     outline-mode-menu-bar-map)
  234.   (define-key outline-minor-mode-map outline-minor-mode-prefix
  235.     outline-mode-prefix-map))
  236.  
  237. (or (assq 'outline-minor-mode minor-mode-map-alist)
  238.     (setq minor-mode-map-alist
  239.       (cons (cons 'outline-minor-mode outline-minor-mode-map)
  240.         minor-mode-map-alist)))
  241.  
  242. ;;;autoload
  243. (defun outline-minor-mode (&optional arg)
  244.   "Toggle Outline minor mode.
  245. With arg, turn Outline minor mode on if arg is positive, off otherwise.
  246. See the command `outline-mode' for more information on this mode."
  247.   (interactive "P")
  248.   (setq outline-minor-mode
  249.     (if (null arg) (not outline-minor-mode)
  250.       (> (prefix-numeric-value arg) 0)))
  251.   (if outline-minor-mode
  252.       (progn
  253.     (make-local-hook 'change-major-mode-hook)
  254.     ;; Turn off this mode if we change major modes.
  255.     (add-hook 'change-major-mode-hook
  256.           '(lambda () (outline-minor-mode -1))
  257.           nil t)
  258.     (make-local-variable 'line-move-ignore-invisible)
  259.     (setq line-move-ignore-invisible t)
  260.     ;; Cause use of ellipses for invisible text.
  261.     (setq buffer-invisibility-spec '((t . t)))
  262.     (run-hooks 'outline-minor-mode-hook))
  263.     (setq line-move-ignore-invisible nil)
  264.     ;; Cause use of ellipses for invisible text.
  265.     (setq buffer-invisibility-spec t))
  266.   ;; When turning off outline mode, get rid of any outline hiding.
  267.   (or outline-minor-mode
  268.       (show-all))
  269.   (force-mode-line-update))
  270.  
  271. (defvar outline-level 'outline-level
  272.   "Function of no args to compute a header's nesting level in an outline.
  273. It can assume point is at the beginning of a header line.")
  274.  
  275. ;; This used to count columns rather than characters, but that made ^L
  276. ;; appear to be at level 2 instead of 1.  Columns would be better for
  277. ;; tab handling, but the default regexp doesn't use tabs, and anyone
  278. ;; who changes the regexp can also redefine the outline-level variable
  279. ;; as appropriate.
  280. (defun outline-level ()
  281.   "Return the depth to which a statement is nested in the outline.
  282. Point must be at the beginning of a header line.  This is actually
  283. the number of characters that `outline-regexp' matches."
  284.   (save-excursion
  285.     (looking-at outline-regexp)
  286.     (- (match-end 0) (match-beginning 0))))
  287.  
  288. (defun outline-next-preface ()
  289.   "Skip forward to just before the next heading line.
  290. If there's no following heading line, stop before the newline
  291. at the end of the buffer."
  292.   (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
  293.              nil 'move)
  294.       (goto-char (match-beginning 0)))
  295.   (if (bolp)
  296.       (forward-char -1)))
  297.  
  298. (defun outline-next-heading ()
  299.   "Move to the next (possibly invisible) heading line."
  300.   (interactive)
  301.   (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
  302.              nil 'move)
  303.       (goto-char (1+ (match-beginning 0)))))
  304.  
  305. (defsubst outline-visible ()
  306.   "Non-nil if the character after point is visible."
  307.   (not (get-char-property (point) 'invisible)))
  308.  
  309. (defun outline-back-to-heading ()
  310.   "Move to previous heading line, or beg of this line if it's a heading.
  311. Only visible heading lines are considered."
  312.   (beginning-of-line)
  313.   (or (outline-on-heading-p)
  314.       (let (found)
  315.     (save-excursion
  316.       (while (not found)
  317.         (or (re-search-backward (concat "^\\(" outline-regexp "\\)")
  318.                     nil t)
  319.         (error "before first heading"))
  320.         (setq found (and (outline-visible) (point)))))
  321.     (goto-char found)
  322.     found)))
  323.  
  324. (defun outline-on-heading-p ()
  325.   "Return t if point is on a (visible) heading line."
  326.   (save-excursion
  327.     (beginning-of-line)
  328.     (and (bolp) (outline-visible)
  329.      (looking-at outline-regexp))))
  330.  
  331. (defun outline-end-of-heading ()
  332.   (if (re-search-forward outline-heading-end-regexp nil 'move)
  333.       (forward-char -1)))
  334.  
  335. (defun outline-next-visible-heading (arg)
  336.   "Move to the next visible heading line.
  337. With argument, repeats or can move backward if negative.
  338. A heading line is one that starts with a `*' (or that
  339. `outline-regexp' matches)."
  340.   (interactive "p")
  341.   (if (< arg 0)
  342.       (beginning-of-line)
  343.     (end-of-line))
  344.   (while (and (not (bobp)) (< arg 0))
  345.     (while (and (not (bobp))
  346.         (re-search-backward (concat "^\\(" outline-regexp "\\)")
  347.                     nil 'move)
  348.         (not (outline-visible))))
  349.     (setq arg (1+ arg)))
  350.   (while (and (not (eobp)) (> arg 0))
  351.     (while (and (not (eobp))
  352.         (re-search-forward (concat "^\\(" outline-regexp "\\)")
  353.                    nil 'move)
  354.         (not (outline-visible))))
  355.     (setq arg (1- arg)))
  356.   (beginning-of-line))
  357.  
  358. (defun outline-previous-visible-heading (arg)
  359.   "Move to the previous heading line.
  360. With argument, repeats or can move forward if negative.
  361. A heading line is one that starts with a `*' (or that
  362. `outline-regexp' matches)."
  363.   (interactive "p")
  364.   (outline-next-visible-heading (- arg)))
  365.  
  366. (defun outline-flag-region (from to flag)
  367.   "Hides or shows lines from FROM to TO, according to FLAG.
  368. If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
  369.   (let ((inhibit-read-only t))
  370.     (save-excursion
  371.       (goto-char from)
  372.       (end-of-line)
  373.       (outline-discard-overlays (point) to 'outline)
  374.       (if flag
  375.       (let ((o (make-overlay (point) to)))
  376.         (overlay-put o 'invisible flag)
  377.         (overlay-put o 'outline t)))))
  378.   (run-hooks 'outline-view-change-hook))
  379.  
  380. ;; Exclude from the region BEG ... END all overlays
  381. ;; with a non-nil PROP property.
  382. ;; Exclude them by shrinking them to exclude BEG ... END,
  383. ;; or even by splitting them if necessary.
  384. ;; Overlays without a non-nil PROP property are not touched.
  385. (defun outline-discard-overlays (beg end prop)
  386.   (if (< end beg)
  387.       (setq beg (prog1 end (setq end beg))))
  388.   (save-excursion
  389.     (goto-char beg)
  390.     (while (< (point) end)
  391.       (let ((overlays (overlays-at (point))))
  392.     (while overlays
  393.       (let ((o (car overlays)))
  394.         (if (overlay-get o prop)
  395.         ;; Either push this overlay outside beg...end
  396.         ;; or split it to exclude beg...end
  397.         ;; or delete it entirely (if it is contained in beg...end).
  398.         (if (< (overlay-start o) beg)
  399.             (if (> (overlay-end o) end)
  400.             (let ((o1 (outline-copy-overlay o)))
  401.               (move-overlay o1 (overlay-start o1) beg)
  402.               (move-overlay o (overlay-start o) beg)))
  403.           (if (> (overlay-end o) end)
  404.               (move-overlay o end (overlay-end o))
  405.             (delete-overlay o)))))
  406.       (setq overlays (cdr overlays))))
  407.       (goto-char (next-overlay-change (point))))))
  408.  
  409. ;; Make a copy of overlay O, with the same beginning, end and properties.
  410. (defun outline-copy-overlay (o)
  411.   (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
  412.               (overlay-buffer o)))
  413.     (props (overlay-properties o)))
  414.     (while props
  415.       (overlay-put o1 (car props) (nth 1 props))
  416.       (setq props (cdr (cdr props))))
  417.     o1))
  418.  
  419. (defun hide-entry ()
  420.   "Hide the body directly following this heading."
  421.   (interactive)
  422.   (outline-back-to-heading)
  423.   (outline-end-of-heading)
  424.   (save-excursion
  425.    (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
  426.  
  427. (defun show-entry ()
  428.   "Show the body directly following this heading."
  429.   (interactive)
  430.   (save-excursion
  431.    (outline-flag-region (point) (progn (outline-next-preface) (point)) nil)))
  432.  
  433. (defun hide-body ()
  434.   "Hide all of buffer except headings."
  435.   (interactive)
  436.   (hide-region-body (point-min) (point-max)))
  437.  
  438. (defun hide-region-body (start end)
  439.   "Hide all body lines in the region, but not headings."
  440.   (save-excursion
  441.     (save-restriction
  442.       (narrow-to-region start end)
  443.       (goto-char (point-min))
  444.       (if (outline-on-heading-p)
  445.       (outline-end-of-heading))
  446.       (while (not (eobp))
  447.     (outline-flag-region (point)
  448.                  (progn (outline-next-preface) (point)) t)
  449.     (if (not (eobp))
  450.         (progn
  451.           (forward-char
  452.            (if (looking-at "\n\n")
  453.            2 1))
  454.           (outline-end-of-heading)))))))
  455.  
  456. (defun show-all ()
  457.   "Show all of the text in the buffer."
  458.   (interactive)
  459.   (outline-flag-region (point-min) (point-max) nil))
  460.  
  461. (defun hide-subtree ()
  462.   "Hide everything after this heading at deeper levels."
  463.   (interactive)
  464.   (outline-flag-subtree t))
  465.  
  466. (defun hide-leaves ()
  467.   "Hide all body after this heading at deeper levels."
  468.   (interactive)
  469.   (outline-back-to-heading)
  470.   (outline-end-of-heading)
  471.   (hide-region-body (point) (progn (outline-end-of-subtree) (point))))
  472.  
  473. (defun show-subtree ()
  474.   "Show everything after this heading at deeper levels."
  475.   (interactive)
  476.   (outline-flag-subtree nil))
  477.  
  478. (defun hide-sublevels (levels)
  479.   "Hide everything but the top LEVELS levels of headers, in whole buffer."
  480.   (interactive "p")
  481.   (if (< levels 1)
  482.       (error "Must keep at least one level of headers"))
  483.   (setq levels (1- levels))
  484.   (save-excursion
  485.     (goto-char (point-min))
  486.     ;; Keep advancing to the next top-level heading.
  487.     (while (or (and (bobp) (outline-on-heading-p))
  488.            (outline-next-heading))
  489.       (let ((end (save-excursion (outline-end-of-subtree) (point))))
  490.     ;; Hide everything under that.
  491.     (outline-flag-region (point) end t)
  492.     ;; Show the first LEVELS levels under that.
  493.     (if (> levels 0)
  494.         (show-children levels))
  495.     ;; Move to the next, since we already found it.
  496.     (goto-char end)))))
  497.  
  498. (defun hide-other ()
  499.   "Hide everything except for the current body and the parent headings."
  500.   (interactive)
  501.   (hide-sublevels 1)
  502.   (let ((last (point))
  503.     (pos (point)))
  504.     (while (save-excursion
  505.          (and (end-of-line 0)
  506.           (not (outline-visible))))
  507.       (save-excursion
  508.     (beginning-of-line)
  509.     (if (eq last (point))
  510.         (progn
  511.           (outline-next-heading)
  512.           (outline-flag-region last (point) nil))
  513.       (show-children)
  514.       (setq last (point)))))))
  515.  
  516. (defun outline-flag-subtree (flag)
  517.   (save-excursion
  518.     (outline-back-to-heading)
  519.     (outline-end-of-heading)
  520.     (outline-flag-region (point)
  521.               (progn (outline-end-of-subtree) (point))
  522.               flag)))
  523.  
  524. (defun outline-end-of-subtree ()
  525.   (outline-back-to-heading)
  526.   (let ((opoint (point))
  527.     (first t)
  528.     (level (funcall outline-level)))
  529.     (while (and (not (eobp))
  530.         (or first (> (funcall outline-level) level)))
  531.       (setq first nil)
  532.       (outline-next-heading))
  533.     (if (bolp)
  534.     (progn
  535.       ;; Go to end of line before heading
  536.       (forward-char -1)
  537.       (if (bolp)
  538.           ;; leave blank line before heading
  539.           (forward-char -1))))))
  540.  
  541. (defun show-branches ()
  542.   "Show all subheadings of this heading, but not their bodies."
  543.   (interactive)
  544.   (show-children 1000))
  545.  
  546. (defun show-children (&optional level)
  547.   "Show all direct subheadings of this heading.
  548. Prefix arg LEVEL is how many levels below the current level should be shown.
  549. Default is enough to cause the following heading to appear."
  550.   (interactive "P")
  551.   (setq level
  552.     (if level (prefix-numeric-value level)
  553.       (save-excursion
  554.         (outline-back-to-heading)
  555.         (let ((start-level (funcall outline-level)))
  556.           (outline-next-heading)
  557.           (if (eobp)
  558.           1
  559.         (max 1 (- (funcall outline-level) start-level)))))))
  560.   (save-excursion
  561.     (save-restriction
  562.       (outline-back-to-heading)
  563.       (setq level (+ level (funcall outline-level)))
  564.       (narrow-to-region (point)
  565.             (progn (outline-end-of-subtree)
  566.                    (if (eobp) (point-max) (1+ (point)))))
  567.       (goto-char (point-min))
  568.       (while (and (not (eobp))
  569.           (progn
  570.             (outline-next-heading)
  571.             (not (eobp))))
  572.     (if (<= (funcall outline-level) level)
  573.         (save-excursion
  574.           (outline-flag-region (save-excursion
  575.                      (forward-char -1)
  576.                      (if (bolp)
  577.                      (forward-char -1))
  578.                      (point))
  579.                    (progn (outline-end-of-heading) (point))
  580.                    nil)))))))
  581.  
  582. (defun outline-up-heading (arg)
  583.   "Move to the heading line of which the present line is a subheading.
  584. With argument, move up ARG levels."
  585.   (interactive "p")
  586.   (outline-back-to-heading)
  587.   (if (eq (funcall outline-level) 1)
  588.       (error "Already at top level of the outline"))
  589.   (while (and (> (funcall outline-level) 1)
  590.           (> arg 0)
  591.           (not (bobp)))
  592.     (let ((present-level (funcall outline-level)))
  593.       (while (not (< (funcall outline-level) present-level))
  594.     (outline-previous-visible-heading 1))
  595.       (setq arg (- arg 1)))))
  596.  
  597. (defun outline-forward-same-level (arg)
  598.   "Move forward to the ARG'th subheading at same level as this one.
  599. Stop at the first and last subheadings of a superior heading."
  600.   (interactive "p")
  601.   (outline-back-to-heading)
  602.   (while (> arg 0)
  603.     (let ((point-to-move-to (save-excursion
  604.                   (outline-get-next-sibling))))  
  605.       (if point-to-move-to
  606.       (progn
  607.         (goto-char point-to-move-to)
  608.         (setq arg (1- arg)))
  609.     (progn
  610.       (setq arg 0)
  611.       (error "No following same-level heading"))))))
  612.  
  613. (defun outline-get-next-sibling ()
  614.   "Move to next heading of the same level, and return point or nil if none."
  615.   (let ((level (funcall outline-level)))
  616.     (outline-next-visible-heading 1)
  617.     (while (and (> (funcall outline-level) level)
  618.         (not (eobp)))
  619.       (outline-next-visible-heading 1))
  620.     (if (< (funcall outline-level) level)
  621.     nil
  622.       (point))))
  623.     
  624. (defun outline-backward-same-level (arg)
  625.   "Move backward to the ARG'th subheading at same level as this one.
  626. Stop at the first and last subheadings of a superior heading."
  627.   (interactive "p")
  628.   (outline-back-to-heading)
  629.   (while (> arg 0)
  630.     (let ((point-to-move-to (save-excursion
  631.                   (outline-get-last-sibling))))
  632.       (if point-to-move-to
  633.       (progn
  634.         (goto-char point-to-move-to)
  635.         (setq arg (1- arg)))
  636.     (progn
  637.       (setq arg 0)
  638.       (error "No previous same-level heading"))))))
  639.  
  640. (defun outline-get-last-sibling ()
  641.   "Move to next heading of the same level, and return point or nil if none."
  642.   (let ((level (funcall outline-level)))
  643.     (outline-previous-visible-heading 1)
  644.     (while (and (> (funcall outline-level) level)
  645.         (not (bobp)))
  646.       (outline-previous-visible-heading 1))
  647.     (if (< (funcall outline-level) level)
  648.     nil
  649.         (point))))
  650.  
  651. (provide 'outline)
  652. (provide 'noutline)
  653.  
  654. ;;; outline.el ends here
  655.